package controllers;

import play.*;
import play.data.validation.Required;
import play.db.jpa.GenericModel.JPAQuery;
import play.mvc.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import org.apache.commons.mail.EmailException;

import models.*;

@With(Secure.class)
// this class is used for the login, Doaa El-Hawary
public class Invitations extends Controller {

	public static void index() {
		render();
	}

	/**
	 * Adds Role of type lead to user but sets title to Organizer and then calls
	 * method in model to invite the user
	 * 
	 * @author Tarek Sheasha
	 * @param invitedId
	 *            , organizationId
	 * @category C1S8
	 * @throws EmailException
	 * @Version 26.02.2011
	 * 
	 */
	public static void inviteUserFullPrivileges(@Required String username,
			long organizationId) throws EmailException {
		User inviter = User.find("byUsername", Security.connected()).first();
		Organization organization = Organization.findById(organizationId);
		User toBeInvited = User.findByUsername(username);
		Role newRole = new Role(organization, "Lead");
		newRole.setTitle("Organizer");
		organization.roles.add(newRole);
		toBeInvited.roleList.add(newRole);
		newRole.setUsers(toBeInvited);
		toBeInvited.save();
		newRole.save();
	    organization.save();
		String invContent = "You have cordially been invited to join Organization, "
				+ organization.getName()
				+ " as an Organizer with full Lead's Privileges.";
		inviter.inviteUser(toBeInvited, invContent, "" + new Date(),
				organization, null, newRole);
		index();
	}

	/**
	 * invited unregistered user into Entity by giving them a Role member but
	 * not yet assigned to entity
	 * 
	 * @author Tarek Sheasha
	 * @version 27.04.2011
	 * @category C1S5
	 * @param inviter
	 * @param userId
	 *            , attributes, entityId
	 * @param entity
	 * @throws EmailException
	 */
	public static void inviteUnregIntoEntity(String fName, String lName,
			@Required String username, @Required String password, String phone, String address,
			@Required String email, long entityId) throws EmailException {

		User inviter = User.find("byUsername", Security.connected()).first();
		Eentity entity = Eentity.findById(entityId);
		LinkedList<String> attributes = new LinkedList<String>();
		attributes.add(fName);
		attributes.add(lName);
		attributes.add(username);
		attributes.add(password);
		attributes.add(phone);
		attributes.add(address);
		attributes.add(email);
		User toBeInvited = new User(attributes);
		Role newRole = new Role(entity, "Member");
		entity.roles.add(newRole);
		newRole.setUsers(toBeInvited);
		toBeInvited.roleList.add(newRole);
		toBeInvited.save();
		newRole.save();
		entity.save();
		String invContent = "You have cordially been invited to join Entity, "
				+ entity.getName() + " as an Ideas' Developer.";

		inviter.inviteUser(toBeInvited, invContent, "" + new Date(), null,
				entity, newRole);
		index();
	}
	/**
	 * Sending Invitation to Unregistered user to be Organizer with full lead
	 * privileges to a certain Organization
	 * 
	 * @author Nirmeen Sallam
	 * @version 2011.0501
	 * @category C1S6
	 * @param Long organizationId, the id of Organization to which the user
	 *         toBeInvited will be set as an Organizer
	 * 
	 * @param String fName , Unregistered User to be invited first name
	 * 
	 * @param String lName , Unregistered User to be invited last name
	 * 
	 * @param String email , Unregistered User to be invited email
	 * 
	 * @return void
	 * @throws EmailException
	 *             ,MalformedURLException
	 */
	public static void sendInvitationToOrganize(long organizationId,
			String fName, String lName, String email)

	throws MalformedURLException, EmailException {
        
		String result = "";
		
		User sender = User.find("byUsername", Security.connected()).first();
		
		System.out.println(User.find("byUsername", Security.connected()).first().toString());
		
		Organization organization = Organization.findById(organizationId);
        
		System.out.println(organization.getName());
		
		String organizationName = organization.getName();
		
		System.out.println(sender.getUserType(organization));
		
		String roleOfSender = sender.getUserType(organization);

		URL urlForResponding = new URL("http://"); // url of the accept and
													// reject view
		if (roleOfSender.equalsIgnoreCase("Lead")) {

			User toBeInvited = new User(fName, lName, email);

			Role newRole = new Role(organization, "Lead");

			newRole.setTitle("Lead");

			String invitationContent = "You're invited to join this organization: "
					+ " "
					+ organizationName
					+ " you can reposnd to the invitation by clicking that link "
					+ urlForResponding;

			sender.inviteUser(toBeInvited, invitationContent, "" + new Date(),
					organization, null, newRole);
        
			result="Your action is done successfully";
			
			sendInvitationToOrganizeResult(result);
		}
         
		result="You are not authhorized to do such an action";
		
		sendInvitationToOrganizeResult(result);

	}
	/**
	 * This methods render to a view that has the Success or Failure message of Sending Invitation to Unregistered User To Organize action
	 * 
	 * @author Nirmeen Sallam
	 * @version 2011.0506
	 * @category C1S6
	 * @param String result : message that have the success or failure of the action
	 * @return void
	 */
	public static void sendInvitationToOrganizeResult(String result){
	
		render(result);
	
	}
	/**
	 * This methods render to a view that has the Form of Sending Invitation to Unregistered User To Organize action
	 * 
	 * @author Nirmeen Sallam
	 * @version 2011.0506
	 * @category C1S6
	 * @param none
	 * @return void
	 */
	public static void sendInvitationToOrganizeButton(){
	
		render();
	
	}

}